# coding: utf-8

from flask import Flask
from flask_cors import CORS
from flask_migrate import Migrate
from flask_script import Manager

import server.services.jwt_fuc as jwt_fuc  # token处理
from exts import db, scheduler, log, jwt, rdb
from server.utils import init_blueprint
from server.utils.dbtools import MP
from server.utils.restful import rest_error
from settings import config


def init_app(env):
    app = Flask(__name__)
    app.config.from_object(config[env or "default"])

    log.init_app(app)

    # 初始化后台任务
    scheduler.init_app(app)
    scheduler.start()

    # 蓝图注册
    init_blueprint(app)

    # 注册勾子刷新令牌
    jwt_fuc.hook(app)

    # 数据库
    db.init_app(app)
    MP.init_app(app)
    rdb.init_app(app)

    # jwt初始化
    jwt.init_app(app)

    CORS(app, resources="*", supports_credentials=True)

    manager = Manager(app)
    Migrate(app, db)

    return manager
